package edu.xaut.bookingsys.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xaut.bookingsys.entity.InfoClass;
import edu.xaut.bookingsys.mapper.InfoClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class InfoClassService extends ServiceImpl<InfoClassMapper, InfoClass> {
    @Autowired
    InfoTeacherService infoTeacherService;

    public List<InfoClass> getAllClasses() {

        List<InfoClass> allClassed = getBaseMapper().selectList(null);
        for (var c : allClassed) {
            c.setTeacher(infoTeacherService.getById(c.getTeacherId()));
        }
        return allClassed;
    }
    public List<InfoClass> getClassesByTeacherId(String teacherId){
        return getBaseMapper().selectList(Wrappers.lambdaQuery(InfoClass.class).eq(InfoClass::getTeacherId,teacherId));
    }

    public boolean insertClasses(List<InfoClass> classes) {
        // 过滤掉 保存的数据异常的
        List<InfoClass> infoClasses = classes.stream().filter(v -> {
            return (
                    !Objects.isNull(v.getTeacherId()) &&
                    (Objects.isNull(v.getTeacher()) || v.getTeacher().getTeacherId() == v.getTeacher().getTeacherId()) &&
                    !Objects.isNull(infoTeacherService.getById(v.getTeacherId())));
        }).collect(Collectors.toList());
        return saveBatch(infoClasses);


    }
}
